# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Tim Stahlhut <stahta01@gmail.com>

_realname=pupnp
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.14.15
pkgrel=1
pkgdesc="Portable SDK for UPnP* Devices (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://github.com/pupnp/pupnp"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-libwinpthread-git")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc" "${MINGW_PACKAGE_PREFIX}-cmake" "${MINGW_PACKAGE_PREFIX}-ninja")
options=('strip' 'staticlibs')
source=(libupnp-${pkgver}.tar.gz::https://github.com/pupnp/pupnp/archive/release-${pkgver}.tar.gz
        001-fix-dll-export.patch
        002-libupnp-win32.patch)
sha256sums=('041a1ac418d228a42a9811b890e21ed4f9dcbd92afef1edb03e7f1dd28320c6e'
            '262a8496205f78cffb6662acafdc9ba35957ff2971d2fd6ef94ff8acaab2ca50'
            '99a0334ff2dd01fdcbe37d7cfbbbc86dacbf2cb7cccd533be3c8833e4367af1c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${srcdir}/pupnp-release-${pkgver}"
  apply_patch_with_msg \
    001-fix-dll-export.patch \
    002-libupnp-win32.patch
}

build() {
  [[ -d "${srcdir}/build-${MSYSTEM}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}"
  mkdir -p "${srcdir}/build-${MSYSTEM}" && cd "${srcdir}/build-${MSYSTEM}"

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    ${extra_config[@]} \
    -DSTATIC_POSTFIX="-static" \
    -DBUILD_TESTING=OFF \
    -Dclient=ON \
    -Ddevice=ON \
    -Dwebserver=ON \
    -Dssdp=ON \
    -Doptssdp=ON \
    -Dsoap=ON \
    -Dgena=ON \
    -Dtools=ON \
    -Dipv6=ON \
    -Dunspecified_server=OFF \
    -Dopen_ssl=OFF \
    -Dblocking_tcp_connections=OFF \
    -Dscriptsupport=ON \
    -Dreuseaddr=ON \
    -Dsamples=ON \
    ../pupnp-release-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest . || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  # License
  install -Dm644 ${srcdir}/pupnp-release-${pkgver}/COPYING ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING
}
